package com.jumper.study.front.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumper.study.common.util.StrUtil;
import com.jumper.study.db.dao.LessonCategoryMapper;
import com.jumper.study.db.dao.LessonChapterMapper;
import com.jumper.study.db.dao.LessonEbookMapper;
import com.jumper.study.db.dao.LessonMapper;
import com.jumper.study.db.model.Lesson;
import com.jumper.study.db.model.LessonCategory;
import com.jumper.study.db.model.LessonChapter;
import com.jumper.study.db.model.LessonEbook;
import com.jumper.study.front.dto.req.LessonSearchReq;
import com.jumper.study.front.dto.resp.LessonCategoryResp;
import com.jumper.study.front.dto.resp.LessonChapterResp;
import com.jumper.study.front.dto.resp.LessonDetailResp;
import com.jumper.study.front.dto.resp.LessonEbookResp;
import com.jumper.study.front.dto.resp.LessonListItemResp;
import com.jumper.study.front.dto.vo.PageVO;
import com.jumper.study.front.helper.LessonHelper;
import com.jumper.study.front.service.LessonService;

@Service("frontLessonService")
public class LessonServiceImpl implements LessonService {

    @Autowired
    private LessonMapper lessonMapper;

    @Autowired
    private LessonCategoryMapper categoryMapper;

    @Autowired
    private LessonChapterMapper chapterMapper;

    @Autowired
    private LessonEbookMapper ebookMapper;

    @Override
    public List<LessonListItemResp> recommendList(int count) {
        LambdaQueryWrapper<Lesson> query = Wrappers.lambdaQuery();
        query.select(Lesson::getId, Lesson::getTitle, Lesson::getThumb, Lesson::getPrice, Lesson::getMarketPrice);
        query.eq(Lesson::getOnself, true);
        query.eq(Lesson::getRecommend, true);
        query.orderByDesc(Lesson::getSort);
        query.last("limit " + count);

        List<Lesson> lessons = lessonMapper.selectList(query);
        List<LessonListItemResp> resp = lessons.stream().map(lesson -> {
            LessonListItemResp item = new LessonListItemResp();
            item.setId(lesson.getId());
            item.setTitle(lesson.getTitle());
            item.setThumb(lesson.getThumb());
            item.setPrice(lesson.getPrice());
            item.setMarketPrice(lesson.getMarketPrice());
            return item;
        }).toList();
        return resp;
    }

    @Override
    public List<LessonCategoryResp> categoryListWithChildren() {
        List<LessonCategory> categories = categoryMapper.selectList(Wrappers.emptyWrapper());
        Map<Integer, LessonCategoryResp> map = new HashMap<>();
        for(LessonCategory category : categories) {
            LessonCategoryResp item = new LessonCategoryResp();
            item.setId(category.getId());
            item.setTitle(category.getTitle());
            if (category.getParentId() == 0) {
                map.put(category.getId(), item);
            } else {
                LessonCategoryResp parent = map.get(category.getParentId());
                parent.addChild(item);
            }
        }

        return map.values().stream().toList();
    }

    @Override
    public List<LessonListItemResp> pageSearchList(PageVO p, LessonSearchReq search) {
        Page<Lesson> page = Page.of(p.getPage(), p.getPageSize());
        LambdaQueryWrapper<Lesson> query = Wrappers.lambdaQuery();
        query.eq(Lesson::getOnself, true);
        if (search.getCategoryId() != null && search.getCategoryId() > 0) {
            query.eq(Lesson::getCategoryId, search.getCategoryId());
        }
        if (!StrUtil.isEmpty(search.getTitle())) {
            query.like(Lesson::getTitle, search.getTitle());
        }
        query.orderBy(true, search.isAsc(), LessonHelper.getSort(search.getSort()));
        List<Lesson> lessons = lessonMapper.selectList(page, query);
        p.setTotal(page.getTotal());

        return lessons.stream().map(lesson -> {
            LessonListItemResp item = new LessonListItemResp();
            item.setId(lesson.getId());
            item.setTitle(lesson.getTitle());
            item.setThumb(lesson.getThumb());
            item.setPrice(lesson.getPrice());
            item.setMarketPrice(lesson.getMarketPrice());
            return item;
        }).toList();
    }

    @Override
    public LessonDetailResp getDetail(Long id) {
        Lesson lesson = lessonMapper.selectById(id);
        if (lesson == null) {
            return null;
        }
        LessonCategory category = categoryMapper.selectById(lesson.getCategoryId());
        LessonDetailResp resp = new LessonDetailResp();
        resp.setId(id);
        resp.setTitle(lesson.getTitle());
        resp.setThumb(lesson.getThumb());
        resp.setSummary(lesson.getSummary());
        resp.setContent(lesson.getContent());
        resp.setPrice(lesson.getPrice());
        resp.setMarketPrice(lesson.getMarketPrice());
        resp.setCategoryId(lesson.getCategoryId());
        resp.setTeachers(lesson.getTeachers());
        resp.setCategoryTitle(category != null ? category.getTitle() : "");

        return resp;
    }

    @Override
    public List<LessonChapterResp> chapterList(Long lessonId) {
        LambdaQueryWrapper<LessonChapter> query = Wrappers.lambdaQuery();
        query.eq(LessonChapter::getLessonId, lessonId);
        query.eq(LessonChapter::getOnself, true);
        List<LessonChapter> chapters = chapterMapper.selectList(query);
        return chapters.stream().map(c -> {
            LessonChapterResp item = new LessonChapterResp();
            item.setId(c.getId());
            item.setTitle(c.getTitle());
            item.setDuration(c.getDuration());

            return item;
        }).toList();
    }

    @Override
    public List<LessonEbookResp> ebookList(Long lessonId) {
        LambdaQueryWrapper<LessonEbook> query = Wrappers.lambdaQuery();
        query.eq(LessonEbook::getLessonId, lessonId);
        query.eq(LessonEbook::getOnself, true);
        List<LessonEbook> ebooks = ebookMapper.selectList(query);
        return ebooks.stream().map(e -> {
            LessonEbookResp item = new LessonEbookResp();
            item.setId(e.getId());
            item.setTitle(e.getTitle());
            item.setFile(e.getTitle());
            item.setSize(0l);
            return item;
        }).toList();
    }

    @Override
    public Lesson getById(Long id) {
        return lessonMapper.selectById(id);
    }
    
}
